<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\UserRequest;
use App\Libs\ResponseMessage;
use App\Models\Users;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class PayController extends Controller
{
    /**
     * 添加用户
     * @param UserRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addUser(UserRequest $request){
        global $g_uid;
        $account    = $request->get('account');
        $password   = $request->get('password');
        $rate       = $request->get('point');
        $parentId   = $g_uid;


        try {
            if (Users::create([
                'account' => $account,
                'password' => Hash::make($password),
                'rate' => $rate,
                'parent_id' => $parentId
            ])) {
                return response()->json(ResponseMessage::getInstance()->success()->response());
            } else {
                return response()->json(ResponseMessage::getInstance()->failed()->response());
            }
        }catch (\Exception $exception){
            return response()->json(ResponseMessage::getInstance()->failed('MOBILE_EXIST')->response());
        }
    }

    /**
     * 登录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function login(Request $request){
        $account    = $request->get('account');
        $password   = $request->get('password');

        $user = Users::where('account', $account)->whereNull('delete_time')->first();

        if(!$user){
            return response()->json(ResponseMessage::getInstance()->failed()->response());
        }

        if(!Hash::check($password, $user->password)){
            return response()->json(ResponseMessage::getInstance()->failed()->response());
        }

        unset($user->password);
        unset($user->delete_time);

        $token = md5($user->account);
        $user->token = $token;
        \Cache::put($token, $user, 24*60);

        return response()->json(ResponseMessage::getInstance()->success($user)->response());
    }

    /**
     * 更新下级的扣点比例
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateRate(Request $request){
        global $g_uid;
        $rate       = $request->get('point');
        $userId     = $request->get('user_id');

        if(!$rate){
            return response()->json(ResponseMessage::getInstance()->failed('PARAM_ERROR')->response());
        }
        if($rate<2 || $rate>6){
            return response()->json(ResponseMessage::getInstance()->failed('PARAM_ERROR')->response());
        }

        $userModel = new Users();
        if(
            \DB::table($userModel->getTable())
                ->where('parent_id', $g_uid)
                ->where('id', $userId)
                ->update([
                    'rate' => $rate
                ]) === false
        ){
            return response()->json(ResponseMessage::getInstance()->failed()->response());
        }


        return response()->json(ResponseMessage::getInstance()->success()->response());
    }


    /**
     * 更新密码
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updatePassword(Request $request){
        global $g_uid;
        $password   = $request->get('password');

        $userModel = new Users();
        if(
            \DB::table($userModel->getTable())
                ->where('id', $g_uid)
                ->update([
                    'password' => Hash::make($password)
                ]) === false
        ){
            return response()->json(ResponseMessage::getInstance()->failed()->response());
        }

        $token = $request->get('token');
        \Cache::forget($token);

        return response()->json(ResponseMessage::getInstance()->success()->response());
    }

    /**
     * 退出登录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function logout(Request $request){
        $token = $request->get('token');
        \Cache::forget($token);
        return response()->json(ResponseMessage::getInstance()->success()->response());
    }

    public function createOrder(Request $request){
        global $g_uuuid;
        $amount = $request->get('amount');
        $payUser = $request->get('pay_user');
        $type = $request->get('type');


    }
}